<template>
  <el-form ref="form" :model="formData" :rules="rules" size="mini" label-width="110px">
    <el-row>
      <el-col :span="24" v-if="operate === 1">
        <el-form-item label="分类ID：">
          <label>{{formData.id}}</label>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="分类名称：" prop="name">
          <el-input placeholder="请输入分类名称" v-model.trim="formData.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="上级分类：" prop="parentId">
          <el-cascader
            :props="classifyProps"
            :options="classifyTree"
            v-model="parentArray"
            change-on-select
            :show-all-levels="false"
            @change="handleChange"
            style="width: 100%">
          </el-cascader>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24" align="center">
        <el-button type="primary" size="small" @click="save">保存</el-button>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import Vue from 'vue'
import axios from '../../axios/index'
import {
  Row,
  Col,
  Form,
  FormItem,
  Input,
  Select,
  Option,
  Button,
  Message
} from 'element-ui'

Vue.use(Row)
Vue.use(Col)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Select)
Vue.use(Option)
Vue.use(Button)

export default {
  props: ['formData', 'operate', 'did'],
  data () {
    return {
      rules: {
        name: [
          {required: true, message: '请输入分类名称', trigger: 'blur'}
        ]
      },
      classifyTree: [],
      classifyProps: {
        value: 'id',
        label: 'name',
        children: 'classifyList'
      },
      parentArray: []
    }
  },
  methods: {
    initData () {
      axios.post('/classify/findClassifyTree')
        .then(res => {
          if (Number(res.errCode) === 0) {
            this.classifyTree = res.list
            this.initParentArray()
          } else {
            Message({showClose: true, message: res.errMsg, type: 'error'})
          }
        })
    },
    initParentArray () {
      let path = this.formData.path
      if (path) {
        this.parentArray = path.split('-').filter(res => {
          return res !== this.formData.id
        })
      }
    },
    handleChange (data) {
      if (data) {
        this.formData.parentId = data[data.length - 1]
      }
    },
    // 保存
    save () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let url = this.operate === 0 ? '/classify/createClassify' : '/classify/updateClassify'
          axios.post(url, this.formData).then(res => {
            if (Number(res.errCode) === 0) {
              this.$emit('hiddenDetailDialog', false)
              Message({showClose: true, message: '保存成功！', type: 'success'})
            } else {
              Message({showClose: true, message: res.errMsg, type: 'error'})
            }
          })
        }
      })
    },
    // 清空表单
    clearValidate () {
      this.parentArray = []
      this.$refs['form'].clearValidate()
    }
  },
  mounted () {
    this.initData()
  },
  watch: {
    did: function (val, oldVal) {
      this.initParentArray()
    }
  }
}
</script>
